﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using Photon.SocketServer;
using log4net.Config;
using MyGameServer.Model;
using MyGameServer.Manager;
using Common;
using MyGameServer.Handler;
namespace MyGameServer
{
    /// <summary>
    /// 
    /// </summary>
    public class MyGameServer : ApplicationBase
    {
        public static MyGameServer Instance { get; private set; }
        public Dictionary<OperationCode, BaseHandler> HandleDict = new Dictionary<OperationCode, BaseHandler>();
        /// <summary>
        /// 通过这个集合可以访问所有客户端，然后才能向所有客户端发送数据
        /// </summary>
        public List<ClientPeer> peerList = new List<ClientPeer>();
        public static readonly ILogger log = LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 当一个客户端连接的时候
        /// </summary>
        /// <param name="initRequest"></param>
        /// <returns></returns>
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            log.Warn("一个客户端连接");
            log.Info("一个客户端连接");
            ClientPeer peer = new ClientPeer(initRequest);
            peerList.Add(peer);
            return peer;
        }
       /// <summary>
       ///初始划
       /// </summary>
        protected override void Setup()
        {
            Instance = this;
            //日志的初始化

            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"]= Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");

            FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath,"log4net.config") );
            if (configFileInfo.Exists) 
            {
                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//让photon知道
                XmlConfigurator.Configure(configFileInfo);//让log4使用配置插件
            }

            #region 测试代码
            //UserManager userManager = new UserManager();
            //User user=     userManager.GetById(1);
            //log.Info(user.Username);
            //log.Info(user.Password);
            //log.Info(user.Id);
            //log.Info(user.Registerdate);
            #endregion


             InitHandler();

            log.Info("初始完成");


        }

        /// <summary>
        /// 服务端关闭的时候
        /// </summary>
        protected override void TearDown()
        {
            log.Info("服务端关闭");
        }

        private void InitHandler() 
        {
            LoginHandler loginHandler = new LoginHandler();
            HandleDict.Add(loginHandler.OpCode, loginHandler);

            DefaultHandler defaultHandler = new DefaultHandler();
            HandleDict.Add(defaultHandler.OpCode, defaultHandler);

            RegisterHandler registerHandler = new RegisterHandler();
            HandleDict.Add(registerHandler.OpCode, registerHandler);

            SyncPostionHanlder syncPostionHanlder = new SyncPostionHanlder();
            HandleDict.Add(syncPostionHanlder.OpCode, syncPostionHanlder);


            SyncPlayerHandler syncPlayerHandler = new SyncPlayerHandler();
            HandleDict.Add(syncPlayerHandler.OpCode, syncPlayerHandler);
        }
    }
}
